package com.zzy.module.tikTok.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzy.common.domain.ParameterErrorBack;
import com.zzy.common.domain.RI;
import com.zzy.common.domain.RS;
import com.zzy.common.domain.RSD;
import com.zzy.common.entity.db.TikTokDaoEntity;
import com.zzy.common.util.HttpsUtils;
import com.zzy.module.tikTok.entity.TikTokAdd;
import com.zzy.module.tikTok.entity.TikTokBack;
import com.zzy.module.tikTok.entity.TikTokResp;
import com.zzy.module.tikTok.mapper.TikTokMapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.zzy.common.finalfied.FinalStr.*;

/**
 * @author zhuZhaoYang
 * @date 2021/12/6 16:54
 */
@RestController
@Validated
@RequestMapping("/tikTok")
public class TikTokController {

    private TikTokMapper tikTokMapper;

    @Autowired
    public void setTikTokMapper(TikTokMapper tikTokMapper) {
        this.tikTokMapper = tikTokMapper;
    }

    @ApiOperation(value = "获取抖音链接列表")
    @GetMapping("/getTikTokList")
    public RI getTikTokList(@Min(value = 1, message = "页码不能小于1")
                            @NotNull(message = "页码不能为空")
                                    Integer index,
                            @Min(value = 1, message = "每页条目不能少于1")
                            @NotNull(message = "每页条目不能为空")
                                    Integer limit) {
        Page<TikTokDaoEntity> page = new Page<>(index, limit);
        IPage<TikTokDaoEntity> tikTokList = tikTokMapper.getTikTokList(page);
        TikTokBack tikTokBack = new TikTokBack();
        tikTokBack.setList(tikTokList.getRecords());
        tikTokBack.setCurIndex(tikTokList.getCurrent());
        tikTokBack.setTotalPage(tikTokList.getPages());
        return new RSD(tikTokBack);
    }

    @ApiOperation(value = "播放抖音视频")
    @GetMapping("/getTikTokVideo")
    public RI getTikTokVideo(@NotNull(message = "id不能为空") Integer id) throws Exception {
        TikTokDaoEntity tikTokDaoEntity = tikTokMapper.getTikTokById(id);
        if (tikTokDaoEntity == null) {
            return new ParameterErrorBack("视频不存在");
        }
        if (tikTokDaoEntity.getMp4() == null) {
            String get = HttpsUtils.get(TIK_TOK_GET + tikTokDaoEntity.getUrl(), null, null);
            TikTokResp tikTokResp = JSON.parseObject(get, TikTokResp.class);
            if (!SUCCESS.equals(tikTokResp.getStatus())) {
                return new ParameterErrorBack("视频解析失败");
            }
            tikTokDaoEntity.setMp4(tikTokResp.getMp4());
            tikTokDaoEntity.setDesc(tikTokResp.getDesc());
            tikTokMapper.updateTikTok(tikTokDaoEntity);
        }
        return new RSD(tikTokDaoEntity);
    }

    @ApiOperation(value = "删除视频")
    @PutMapping("/deleteVideo")
    public RI deleteVideo(@NotNull(message = "id不能为空") Integer id) {
        tikTokMapper.deleteById(id);
        return new RS();
    }

    @ApiOperation(value = "添加抖音视频")
    @PostMapping("/addVideo")
    public RI addVideo(@RequestBody TikTokAdd tikTokAdd) throws Exception {
        Pattern pattern = Pattern.compile(URL_REX);
        Matcher matcher = pattern.matcher(tikTokAdd.getUrl());
        if (!matcher.find()) {
            return new ParameterErrorBack("url匹配失败");
        }
        String url = matcher.group();
        String get = HttpsUtils.get(TIK_TOK_GET + url, null, null);
        TikTokResp tikTokResp = JSON.parseObject(get, TikTokResp.class);
        if (!SUCCESS.equals(tikTokResp.getStatus())) {
            return new ParameterErrorBack("视频解析失败");
        }
        TikTokDaoEntity tikTokDaoEntity = new TikTokDaoEntity();
        tikTokDaoEntity.setUrl(url);
        tikTokDaoEntity.setDesc(tikTokResp.getDesc());
        tikTokDaoEntity.setMp4(tikTokResp.getMp4());
        tikTokMapper.myInsert(tikTokDaoEntity);
        return new RS("添加成功");
    }
}
